package com.example.port.service.impl;

import com.example.port.entity.StorageRecord;
import com.example.port.mapper.StorageRecordMapper;
import com.example.port.service.StorageRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StorageRecordServiceImpl implements StorageRecordService {
    
    private final StorageRecordMapper storageRecordMapper;
    
    @Autowired
    public StorageRecordServiceImpl(StorageRecordMapper storageRecordMapper) {
        this.storageRecordMapper = storageRecordMapper;
    }
    
    @Override
    public int insertStorageRecord(StorageRecord record) {
        // 检查是否已设置ID，如果没有，则自动生成ID
        if (record.getRecordId() == null) {
            Long maxId = storageRecordMapper.getMaxRecordId();
            record.setRecordId(maxId + 1);
        }
        return storageRecordMapper.insert(record);
    }
    
    @Override
    public List<StorageRecord> getAllStorageRecords() {
        return storageRecordMapper.selectAll();
    }
    
    @Override
    public StorageRecord getStorageRecordById(Long recordId) {
        return storageRecordMapper.selectById(recordId);
    }
    
    @Override
    public int updateStorageRecord(StorageRecord record) {
        return storageRecordMapper.update(record);
    }
    
    @Override
    public int deleteStorageRecord(Long recordId) {
        return storageRecordMapper.delete(recordId);
    }
    
    @Override
    public List<StorageRecord> getStorageRecordsByCargoId(Integer cargoId) {
        return storageRecordMapper.selectByCargoId(cargoId);
    }
    
    @Override
    public int deleteStorageRecordsByCargoId(Integer cargoId) {
        return storageRecordMapper.deleteByCargoId(cargoId);
    }
} 
